import 'package:flutter/material.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class NNPopupRoute<T> extends PopupRoute<T> {
  @override
  Color? get barrierColor => null;

  @override
  bool get barrierDismissible => false;

  @override
  String? get barrierLabel => null;

  @override
  Duration get transitionDuration => Duration(seconds: 0);

  /// backgroudView Color
  final Color? backgroudViewColor;

  /// child'alignment, default value: [Alignment.center]
  final Alignment alignment;

  /// child
  final Widget child;

  /// backgroudView action
  final Function onClick;

  final bool needSafe;

  NNPopupRoute(
      {this.backgroudViewColor,
      this.alignment = Alignment.center,
      required this.needSafe,
      required this.onClick,
      required this.child});

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    final screenSize = MediaQuery.of(context).size;

    Widget getContent() {
      if (needSafe) {
        return SafeArea(
          bottom: false,
          child: Align(
            alignment: alignment,
            child: child,
          ),
        );
      } else {
        return Align(
          alignment: alignment,
          child: child,
        );
      }
    }

    return Material(
      color: Colors.transparent,
      child: GestureDetector(
        child: Stack(
          children: [
            Container(
              width: screenSize.width,
              height: screenSize.height,
              color: backgroudViewColor ?? Colors.black.withOpacity(0.3),
            ),
            getContent(),
          ],
        ),
        onTap: ClickUtils.debounce(() {
          onClick();
        }),
      ),
    );
  }
}
